// pages/addDiary/addDiary.js
import Toast from '../../dist/toast/toast';
import {
  lastsleepdiary,
  commitsleepdiary
} from '../../api/index.js'


const ages = []
const hours = []
const minutes = []
for (let i = 13; i <= 120; i++) {
  ages.push(i + '')
}
for (let i = 0; i <= 23; i++) {
  if (i < 10) {
    hours.push('0' + i)
  } else {
    hours.push('' + i)
  }
}
for (let i = 0; i <= 59; i++) {
  if (i < 10) {
    minutes.push('0' + i)
  } else {
    minutes.push('' + i)
  }

}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: [],
    valueSex:[],
    //星期几
    week: '',
    //性别
    sex: ['女', '男'],
    //记录日期
    day: '',
    //年龄
    ages: ages,
    //午睡
    siesta: ['0', '10', '20', '30', '40', '50', '60', '70', '80', '90', '100', '120', '130', '140', '150', '160'],
    //睡前服药
    medicine: '',
    //小时
    hours: hours,
    //分钟
    minutes: minutes,
    //早上醒来
    wakeUp: '00:00',
    //早上起床
    getUp: '00:00',
    //多久睡着
    howLong: ['10', '20', '30', '40', '50', '60', '70', '80', '90', '100', '120', '130', '140', '150', '160'],
    //夜醒次数
    wakingHours: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'],
    //夜醒总时长
    totalWakingHours: ['0', '10', '20', '30', '40', '50', '60', '70', '80', '90', '100', '120', '130', '140', '150', '160', '180'],
    result: [{
        value: 0
      },
      {
        value: 0
      },
      {
        value: 0
      },
      {
        value: 0,
        value1: 0
      },
      {
        value: 0
      },
      {
        value: 0
      },
      {
        value: 0
      },
      {
        value: 0,
        value1: 0
      },
      {
        value: 0,
        value1: 0
      }
    ],


  },


  bindChange(e) {
    let arr = this.data.result
    arr[e.target.dataset.index].value = e.detail.value[0]
    arr[e.target.dataset.index].value1 = e.detail.value[1]
    this.setData({
      result: arr
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getLastsleepdiary()
    this.setData({
      day: this.getDate(),
      week: this.getWeekDate(),
    })
  },
  //获取上次睡眠日记信息
  getLastsleepdiary() {
    lastsleepdiary().then(res => {
      if (res.errcode == '00000' && res.last_diary != '') {
        let last_diary = res.last_diary
        last_diary = JSON.parse(last_diary.replace(/'/g, '"'))
        let arr = Object.keys(last_diary, 'last_diary')
        let value=[]
        let valueSex=[]
        let index= this.getIndex(this.data.ages, last_diary[arr[3]])
        let sexIndex = this.getIndex(this.data.sex, last_diary[arr[2]])
        value.push(index)
        valueSex.push(sexIndex)
        this.setData({
          value,
          valueSex
        })
      }
    })
  },

getIndex(arr,str){
  for (var i in arr) {
    if (arr[i] == str) {
      return i;
    };
  };
},
  



  getDate() {
    let date = new Date();
    date.setTime(date.getTime() - 24 * 60 * 60 * 1000);
    let month = date.getMonth() + 1
    let day = date.getDate()
    if (month.toString().length == 1) {
      month = '0' + month
    }
    if (day.toString().length == 1) {
      day = '0' + day
    }
    return date.getFullYear() + '-' + month + "-" + day;
  },


  getWeekDate() {
    var now = new Date();
    var day = now.getDay();
    var weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
    var week = weeks[day - 1];
    var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
    var week = weeks[yesterday.getDay()];
    return week;
  },


  //上床时间
  bindTimeChange(e) {
    this.setData({
      bedTimes: e.detail.value
    })
  },

  //早上醒来
  bindWakeUpChange(e) {
    this.setData({
      wakeUp: e.detail.value
    })
  },

  //早上起床
  bindGetUpChange(e) {
    console.log(e)
    this.setData({
      getUp: e.detail.value
    })
  },
  handelInput(e) {
    this.setData({
      medicine: e.detail.value
    })

  },

  nextStep() {
    let data = {
      "记录日期": this.data.day,
      "星期几": this.data.week,
      "性别": this.data.sex[this.data.result[0].value],
      "年龄": this.data.ages[this.data.result[1].value],
      "午睡": this.data.siesta[this.data.result[2].value],
      "睡前服药": this.data.medicine,
      "上床时间": this.data.hours[this.data.result[3].value] + ':' + this.data.minutes[this.data.result[3].value1],
      "多久睡着": this.data.howLong[this.data.result[4].value],
      "夜醒次数": this.data.wakingHours[this.data.result[5].value],
      "夜醒总时长": this.data.totalWakingHours[this.data.result[6].value],
      "早上醒来": this.data.hours[this.data.result[7].value] + ':' + this.data.minutes[this.data.result[7].value1],
      "早上起床": this.data.hours[this.data.result[8].value] + ':' + this.data.minutes[this.data.result[8].value1]

    }
    commitsleepdiary(data).then(res => {
      if (res.errcode == '00000') {
        wx.navigateTo({
          url: '/pages/course/course'
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  //分享
  onShareAppMessage: function () {
    return {
      title: '我正在使用SMART睡眠训练计划，超有用！推荐给你',
      imageUrl: '../../images/98875.png'

    }
  },
})